const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId

const DB_URL = 'mongodb://localhost:27017/boss'
mongoose.connect(DB_URL)

if (process.env.NODE_ENV !== 'production') {
  mongoose.set('debug', true)
}

mongoose.connection.on('connected', function () {
  console.log('mongo connect success')
})
mongoose.connection.on('error', function () {
  console.log('mongodb connect error')
});

// schema
const models = {
  User: {
    user: {
      type: String,
      required: true
    },
    pwd: {
      type: String,
      required: true
    },
    type: {
      type: String,
      required: true
    },
    avatar: String,
    desc: String,
    // 职位或招聘岗位
    title: String,
    // boss 含以下两个字段
    company: String,
    money: String
  },
  Chat: {
    chatid: {
      type: String,
      required: true
    },
    read: {
      type: Boolean,
      default: false
    },
    from: {
      type: ObjectId,
      ref: 'User'
    },
    to: {
      type: ObjectId,
      ref: 'User'
    },
    content: {
      type: String,
      required: true
    },
    ctime: {
      type: Number,
      default: new Date().getTime()
    }
  }
}

// 注册模型
for(var m in models) {
  mongoose.model(m, new Schema(models[m]))
}